package com.gitee.wsl.ext.string

import com.gitee.wsl.ext.base.isBlankChar


// containsAny ---------------------------------------------------------------------------------------------------------

/**
 * Returns `true` if this character sequence contains any of the specified [others] as a substring.
 *
 * @param ignoreCase `true` to ignore the character case when comparing strings. By default `false`.
 */
 fun <T : CharSequence> CharSequence.containsAny(others: Iterable<T>, ignoreCase: Boolean = false): Boolean =
    others.any { contains(it, ignoreCase = ignoreCase) }

/**
 * Returns `true` if this character sequence contains any of the specified [others] as a substring.
 *
 * @param ignoreCase `true` to ignore the character case when comparing strings. By default `false`.
 */
 fun <T : CharSequence> CharSequence.containsAny(vararg others: T, ignoreCase: Boolean = false): Boolean =
    others.any { contains(it, ignoreCase = ignoreCase) }

/**
 * 检查指定字符串中是否只包含给定的字符<br></br>
 * 这里的containsOnly并不是必须全部给定的testChars都需要有，而是一个子集。testChars是个限定集合，检查字符串中的字符是否在这个限定集合中。<br></br>
 * <pre>`StrUtil.containsOnly("asdas", 'a', 'd', 's','l');   --> true
`</pre> *
 *
 * @param str       字符串
 * @param testChars 检查的字符
 * @return 字符串含有非检查的字符，返回false
 * @since 4.4.1
 */
fun CharSequence.containsOnly(vararg testChars: Char): Boolean {
    if (!isEmpty()) {
        val len = length
        for (i in 0..<len) {
            if (!testChars.contains(get(i))) {
                return false
            }
        }
    }
    return true
}

/**
 * 检查指定字符串中是否含给定的所有字符串
 *
 * @param str       字符串
 * @param testChars 检查的字符
 * @return 字符串含有非检查的字符，返回false
 * @since 4.4.1
 */
fun CharSequence.containsAll(vararg testChars: CharSequence): Boolean {
    if (isBlank() || testChars.isEmpty()) {
        return false
    }
    for (testChar in testChars) {
        if (!contains( testChar)) {
            return false
        }
    }
    return true
}

/**
 * 给定字符串是否包含空白符（空白符包括空格、制表符、全角空格和不间断空格）<br></br>
 * 如果给定字符串为null或者""，则返回false
 *
 * @param str 字符串
 * @return 是否包含空白符
 * @since 4.0.8
 */
fun CharSequence.containsBlank(): Boolean {
    if (isEmpty()) {
        return false
    }

    var i = 0
    while (i < length) {
        if (get(i).isBlankChar()) {
            return true
        }
        i += 1
    }
    return false
}